package com.inossem.warehousestorage_inossem.activity.inventory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inventory.InventoryOverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.inventory.InventoryAreaBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.InventoryManageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InventoryUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

/**
 * Created by Administrator on 2017/8/12 0012.
 */

public class InventoryOverViewActivity extends BaseActivity {

    private TextView title;
    private PullableListView list;
    private InventoryOverviewAdapter adapter;

    private TextView nextBtn;

    private InventoryAreaBean.DataBean bean;

    private String inventoryVoucher;
    private String voucherType;
    private String factoryCode;
    private String inventoryAddressCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        SPUtils.removeInformation(this, SPUtils.KEY_INVENTORY_INFORMATION);
        setContentView(R.layout.activity_inventory_overview);
        initView(this);
        initData(this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextBtn = (TextView) activity.findViewById(R.id.nextBtn);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_inventory_manage_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        list = (PullableListView) activity.findViewById(R.id.list);
        list.setPullDownFlag(false);
        list.setPullUpFlag(false);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_inventory_overview_title));
        inventoryVoucher = activity.getIntent().getStringExtra(InventoryExtra.INVENTORY_VOUCHER);
        voucherType = activity.getIntent().getStringExtra(InventoryExtra.VOUCHER_TYPE);
        factoryCode = activity.getIntent().getStringExtra(InventoryExtra.FACTORY_CODE);
        inventoryAddressCode = activity.getIntent().getStringExtra(InventoryExtra.INVENTORY_ADDRESS_CODE);

//        getData(activity);

        nextBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                if (InventoryUtils.getTotalCheckCount(bean) <= 0) {
//                    Utils.middleToast(activity, "盘点数不能为空");
//                    return;
//                } else {
                    Intent intent = new Intent(activity, InventoryCommitActivity.class);
                    intent.putExtra(InventoryExtra.INVENTORY_VOUCHER, inventoryVoucher);
                    activity.startActivity(intent);
//                }
            }
        });

        setmScanListener((String scanResult, int length) -> {
            String position = QRCodeUtils.DecryptQRCode(scanResult);
//            String an = position.substring(0, position.lastIndexOf("-"));
//            an = an.replaceFirst("-", " ");
//            adapter.doSeach(position);
            adapter.doSeach(position);
        }, QRCodeConstant.TYPE_POSITION);

    }

    public PullableListView getList() {
        return list;
    }

    public void setList(PullableListView list) {
        this.list = list;
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    protected void onResumeImpl() {
        getData(this);
    }

    private void getData(Activity activity) {
        InventoryManageRequest.overview(activity, inventoryVoucher, new GsonBeanListener<InventoryAreaBean>() {
            @Override
            public void onSuccess(InventoryAreaBean bean) {
                bean.getData().setFactoryCode(factoryCode);
                bean.getData().setInventoryAddressCode(inventoryAddressCode);
                bean.getData().setInventoryVoucher(inventoryVoucher);
                bean.getData().setVoucherType(voucherType);
                if (adapter == null) {
                    adapter = new InventoryOverviewAdapter(InventoryOverViewActivity.this, bean.getData());
                    getList().setAdapter(adapter);
                } else {
                    adapter.setBean(bean.getData());
                    adapter.notifyDataSetChanged();
                }

                InventoryOverViewActivity.this.bean = bean.getData();
                SPUtils.saveInformation(activity, SPUtils.KEY_INVENTORY_INFORMATION, InventoryOverViewActivity.this.bean);

            }
        });

    }

}
